﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class UserControl_ucRegister : System.Web.UI.UserControl
{
    TblCustomersDAO cusDAO = new TblCustomersDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private bool CheckExistEmail()
    {
        bool flag = false;
        try
        {
            flag = cusDAO.CheckEmail(txtUsername.Text, 1);
           
        }
        catch ( Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }
        return flag;

    }
    protected void cmdRegister_Click(object sender, EventArgs e)
    {
        bool bFlag = false;
        lblMessage.Text = String.Empty;
        msCaptcha.ValidateCaptcha(txtCaptcha.Text);
        if (!msCaptcha.UserValidated)
        {
            lblMessage.Text = "The characters you entered didn't match the Verificaton code";
            txtCaptcha.Text = String.Empty;
            lblMessage.ForeColor = System.Drawing.Color.Red;
            return;
        }
        if (CheckExistEmail())
        {
            lblMessage.Text = "The email address already existed.";
            txtCaptcha.Text = String.Empty;
            return;
        }

        try
        {
          //  EmailService emailSrv = new EmailService();

            TblCustomers customer = new TblCustomers();         
            customer.Emailaddress = txtUsername.Text;
            customer.Emailpassword = Encryption.EncryptToString(txtPassword.Text);
            customer.DateCreated = DateTime.Now;
            customer.PreferContactMethod = 2;
            customer.CustomerFirstname = txtFName.Text;
            customer.CustomerLastname = txtLName.Text;
            customer.IsAdmin = Convert.ToBoolean("False");
            DataTable dt = cusDAO.InsUpdCustomers(customer, 1);

            foreach (DataRow dr in dt.Rows)
            {
                customer.CustomerIndex =Convert.ToInt32(dr["CustomerIndex"]);
            }
            //Send mail     
            //user = userDAO.GetAll(user.Email);
            //string actLink = "http://" + Request.Url.Host + "//Activate.aspx?aid=" + Encryption.EncryptToString(user.AccountID.ToString()) + "&ac=" + Encryption.EncryptToString(user.ActivatedCode);

            //emailSrv.SendWelcomeMembershipEmail(user, Server.MapPath(Global.MEMBERSHIP_WELCOME), actLink);
            Session[Global.SESSION_LOGGED_USER] = customer;
            bFlag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }
        if (bFlag)
        {
            //lblMessage.Text = "Your account has been registered successfully.<br /><br />Please check your email <b><u>" + txtUsername.Text.Trim() + "</u></b> to activate before logging the website.";
            //lblMessage.ForeColor = System.Drawing.Color.Blue;
            //Panel1.Visible = false;
           // Response.Redirect("Default1.aspx");
            //UserControl control = (UserControl)this.Page.FindControl("ucRegister1");
            //control.Visible = false;

            Response.Redirect("~/Admin/Customer.aspx");
            
        }
    }
}
